using System.IO;
using System.Linq;
using RapidDownloader.Model;

namespace RapidDownloader.Modules.DownloadModule.ViewModels
{
    sealed partial class NewDownload
    {
        #region Fields

        private bool _CanStartDownload;

        #endregion

        #region Constructors

        public NewDownload()
        {
            DownloadTitle = "New download";

            Subscriptions.Subscribe(Module.Context.InvalidateCanDownload, InvalidateCanDownload);
        }

        #endregion

        #region Properties

        public string DownloadTitle
        {
            get
            {
                return ModuleTitle;
            }
            set
            {
                ModuleTitle = value;
                SendPropertyChanged("DownloadTitle");
            }
        }

        public bool CanDownload
        {
            get
            {
                return _CanStartDownload;
            }
            private set
            {
                _CanStartDownload = value;
                SendPropertyChanged("CanStartDownload");
            }
        }

        #endregion

        #region Command Methods

        public void Download()
        {
            Module.OpenPage("Download", ModuleTitle);
        }

        #endregion

        #region Private Methods

        private void InvalidateCanDownload(DownloadContext context)
        {
            var files = context.Files;
            CanDownload = !string.IsNullOrWhiteSpace(context.DownloadFolder) && Directory.Exists(context.DownloadFolder) && files.Any() && files.All(f => f.CanDownload == true);
        }

        #endregion
    }
}